<template>
    <div>
        <headers />
        <div class="container">
            <div>
                <div class="userCenterWarp">
                    <div class="userCenterTitle">供应商入驻</div>
                    <template v-if="showNotice">
                        <el-result :icon="statusIcon" :title="statusText"></el-result>
                    </template>
                    <template v-else>
                        <div class="header">
                            基本信息
                        </div>
                        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px"
                            style="width: 750px;">
                            <el-form-item label="登录账号" prop="username">
                                <el-input v-model="ruleForm.username" placeholder="请输入登录账号"></el-input>
                            </el-form-item>
                            <el-form-item label="登录密码" prop="password">
                                <el-input type="password" v-model="ruleForm.password" placeholder="请输入登录密码"></el-input>
                            </el-form-item>
                            <el-form-item label="供应商名称" prop="merchant_name">
                                <el-input v-model="ruleForm.merchant_name" placeholder="请输入供应商名称"></el-input>
                            </el-form-item>
                            <el-form-item label="手机号码" prop="mobile">
                                <el-input v-model="ruleForm.mobile" placeholder="请输入手机号码"></el-input>
                            </el-form-item>
                            <el-form-item label="餐饮店区域" prop="provinceCityArea">
                                <el-cascader ref="cascaderAddr" v-model="ruleForm.provinceCityArea" :options="regions"
                                    @change="handleChange"></el-cascader>
                            </el-form-item>
                            <el-form-item label="详细地址" prop="address">
                                <el-input v-model="ruleForm.address" placeholder="请输入详细地址"></el-input>
                            </el-form-item>
                            <el-form-item label="门店logo" prop="logo">
                                <div class="logo-uploader">
                                    <uploadFile v-model="ruleForm.logo" :limit="1" :multiple="false">
                                        <template v-slot:upBtn>
                                            <img v-if="ruleForm.logo" :src="ruleForm.logo" class="logo">
                                            <i v-else class="el-icon-plus logo-uploader-icon"></i>
                                        </template>
                                    </uploadFile>
                                </div>
                            </el-form-item>

                            <div class="header mt-60">
                                资质信息
                            </div>
                            <el-form-item label="法人姓名" prop="realname">
                                <el-input v-model="ruleForm.realname" placeholder="请输入法人姓名"></el-input>
                            </el-form-item>
                            <el-form-item label="身份证号" prop="card_no">
                                <el-input v-model="ruleForm.card_no" placeholder="请输入身份证号"></el-input>
                            </el-form-item>

                            <el-form-item label="身份证正反面" prop="card_cert">
                                <el-row>
                                    <el-col :span="12">
                                        <div class="avatar-uploader idCardFront">
                                            <uploadFile v-model="ruleForm.logo" :limit="1" :multiple="false">
                                                <template v-slot:upBtn>
                                                    <img v-if="ruleForm.front_card" :src="ruleForm.front_card"
                                                        class="avatar">
                                                    <i v-else class="el-icon-plus avatar-uploader-icon" />
                                                </template>

                                                <template v-slot:tip>
                                                    <div class="el-upload__tip">身份证人像面</div>
                                                </template>
                                            </uploadFile>
                                        </div>
                                    </el-col>
                                    <el-col :span="12">
                                        <div class="avatar-uploader idCardReverse">
                                            <uploadFile v-model="ruleForm.logo" :limit="1" :multiple="false">
                                                <template v-slot:upBtn>
                                                    <img v-if="ruleForm.behind_card" :src="ruleForm.behind_card"
                                                        class="avatar">
                                                    <i v-else class="el-icon-plus avatar-uploader-icon" />
                                                </template>

                                                <template v-slot:tip>
                                                    <div class="el-upload__tip">身份证国徽面</div>
                                                </template>
                                            </uploadFile>
                                        </div>
                                    </el-col>
                                </el-row>
                            </el-form-item>

                            <el-form-item label="营业执照" prop="business">
                                <div class="avatar-uploader businessLicense">
                                    <uploadFile v-model="ruleForm.business" :limit="1" :multiple="false">
                                        <template v-slot:upBtn>
                                            <img v-if="ruleForm.business" :src="ruleForm.business" class="avatar">
                                            <i v-else class="el-icon-plus avatar-uploader-icon" />
                                        </template>
                                    </uploadFile>
                                </div>
                            </el-form-item>

                            <div class="header mt-60">
                                收款信息
                            </div>
                            <el-form-item label="开户名称" prop="bank_name">
                                <el-input v-model="ruleForm.bank_name" placeholder="请输入开户名称"></el-input>
                            </el-form-item>
                            <el-form-item label="银行账号" prop="bank_no">
                                <el-input v-model="ruleForm.bank_no" placeholder="请输入银行账号"></el-input>
                            </el-form-item>
                            <el-form-item label="开户银行" prop="bank_open">
                                <el-input v-model="ruleForm.bank_open" placeholder="请输入开户银行"></el-input>
                            </el-form-item>

                            <el-form-item>
                                <el-button type="danger" class="custom-btn"
                                    @click="submitForm('ruleForm')">提交</el-button>
                            </el-form-item>
                        </el-form>
                    </template>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { mapGetters } from 'vuex'
import test from '@/utils/test'
import headers from '@/components/header.vue'
import uploadFile from '@/components/uploadFile.vue'
import { getMerchantDetail, merchantEnter } from '@/api/apis'
export default {
    name: "merchant",
    components: {
        headers,
        uploadFile
    },
    data () {
        // 验证手机号的规则
        var validateMobile = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('手机号码不能为空'))
            } else if (!test.mobile(value)) {
                callback(new Error('请输入正确的手机号码'))
            } else {
                callback()
            }
        };
        // 验证身份证号码的规则
        var validateIdCard = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('身份证号码不能为空'))
            } else if (!test.idCard(value)) {
                callback(new Error('请输入正确的身份证号码'))
            } else {
                callback()
            }
        };
        return {
            showNotice: false,
            statusText: '审核中...',
            statusIcon: '',// 0审核中，1通过，2拒绝
            ruleForm: {
                provinceCityArea: '', // 供应商区域
                username: 'a123456',
                password: 'a123456',
                merchant_name: '供应商名称',
                mobile: '17318556553',
                province: '天津',
                city: '市辖区',
                area: '河东区',
                address: '站前路宝文商务大厦2501',
                logo: '',
                lat: '',
                long: '',
                realname: '法人姓名',
                card_no: '340823199001251016',
                front_card: 'front_card',
                behind_card: 'behind_card',
                business: 'business',
                bank_name: '开户名称',
                bank_no: '6222021302003074360',
                bank_open: '中国工商银行黄山路支行,',
            },
            rules: {
                mobile: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    {
                        validator: validateMobile,
                        trigger: ['change', 'blur']
                    }
                ],
                card_no: [{
                    required: true,
                    message: '请输入身份证号码',
                    trigger: ['change', 'blur']
                },
                {
                    validator: validateIdCard,
                    trigger: ['change', 'blur']
                }
                ],
                username: [{
                    required: true,
                    message: '请输入登录账号',
                    trigger: ['change', 'blur']
                }],
                password: [{
                    required: true,
                    message: '请输入登录密码',
                    trigger: ['change', 'blur']
                }],
                merchant_name: [{
                    required: true,
                    message: '请输入供应商名称',
                    trigger: ['change', 'blur']
                }],
                provinceCityArea: [{
                    required: true,
                    message: '请选择所在省市区',
                    trigger: ['change', 'blur']
                }],
                province: [{
                    required: true,
                    message: '请选择所在省',
                    trigger: ['change', 'blur']
                }],
                city: [{
                    required: true,
                    message: '请选择所在城市',
                    trigger: ['change', 'blur']
                }],
                area: [{
                    required: true,
                    message: '请选择所在区',
                    trigger: ['change', 'blur']
                }],
                address: [{
                    required: true,
                    message: '请输入门店详细地址',
                    trigger: ['change', 'blur']
                }],
                realname: [{
                    required: true,
                    message: '请输入法人姓名',
                    trigger: ['change', 'blur']
                }],
                card_no: [{
                    required: true,
                    message: '请输入身份证号',
                    trigger: ['change', 'blur']
                }],
                /* front_card: [{
                    required: true,
                    message: '请上传身份证人像面',
                    trigger: ['change', 'blur']
                }],
                behind_card: [{
                    required: true,
                    message: '请上传身份证国徽面',
                    trigger: ['change', 'blur']
                }],
                business: [{
                    required: true,
                    message: '请上传营业执照',
                    trigger: ['change', 'blur']
                }], */
                bank_name: [{
                    required: true,
                    message: '请输入开户名称',
                    trigger: ['change', 'blur']
                }],
                bank_no: [{
                    required: true,
                    message: '请输入银行账号',
                    trigger: ['change', 'blur']
                }],
                bank_open: [{
                    required: true,
                    message: '请输入开户银行',
                    trigger: ['change', 'blur']
                }
                ]
            }
        }
    },
    computed: {
        ...mapGetters(['regions']),
    },
    mounted () {
        this.getMerchantData()
    },
    methods: {
        getMerchantData(){
            getMerchantDetail().then(res => {
                this.showNotice = res.code === 1 ? true : false;
                this.statusText = res.msg;
                if (res.code === 1) {
                    let status = res.data.status;
                    if (status === 0) {
                        this.statusIcon = 'warning'
                    }else if (status === 1) {
                        this.statusIcon ='success'
                    }else {
                        this.statusIcon = 'error'
                    }
                } else {
                    this.$message.error(res.msg);
                }
            })
        },
        handleChange (item) {
            const [province, city, area] = this.$refs['cascaderAddr'].getCheckedNodes()[0].pathLabels;
            this.ruleForm.province = province;
            this.ruleForm.city = city;
            this.ruleForm.area = area;
            //this.ruleForm.buy_address = this.$refs['cascaderAddr'].getCheckedNodes()[0].pathLabels.join('')
        },
        handleAvatarSuccess (res, file) {
            this.imageUrl = URL.createObjectURL(file.raw)
        },
        beforeAvatarUpload (file) {
            const isJPG = file.type === 'image/jpeg'
            const isLt2M = file.size / 1024 / 1024 < 2

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!')
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!')
            }
            return isJPG && isLt2M
        },
        // 文件发生变化时
        changePictureUploadIDFront (file, fileList) {
            this.ruleForm.card_cert = URL.createObjectURL(file.raw)
            const isJPG = file.raw.type === 'image/jpeg'
            const isPNG = file.raw.type === 'image/png'
            const isLt2M = file.raw.size / 1024 / 1024 < 2

            if (file.raw) {
                if (isJPG || isPNG) {
                    const fileFormData = new FormData()
                    fileFormData.append('files', file.raw)
                    addIdCard(fileFormData).then(res => { //调用上传接口
                        if (res) {
                            this.ruleForm.card_cert = URL.createObjectURL(file.raw)//接口调用成功后赋值src
                            this.$message({
                                message: '恭喜你，上传成功！',
                                type: 'success'
                            })
                        }
                    })
                    this.ruleForm.card_cert = URL.createObjectURL(file.raw)
                } else {
                    this.$message.error('上传图片只能是 JPG或PNG 格式!')
                }
            }

            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 2MB!')
            }
            // 返回图是jpg或png格式并且大小不超过2MB
            return (isJPG || isPNG) && isLt2M
        },
        changePictureUploadIDReverse (file, fileList) {
            this.ruleForm.card_reverse_cert = URL.createObjectURL(file.raw)
            const isJPG = file.raw.type === 'image/jpeg'
            const isPNG = file.raw.type === 'image/png'
            const isLt2M = file.raw.size / 1024 / 1024 < 2

            if (file.raw) {
                if (isJPG || isPNG) {
                    const fileFormData = new FormData()
                    fileFormData.append('files', file.raw)
                    addIdCard(fileFormData).then(res => { //调用上传接口
                        if (res) {
                            this.ruleForm.card_reverse_cert = URL.createObjectURL(file.raw)//接口调用成功后赋值src
                            this.$message({
                                message: '恭喜你，上传成功！',
                                type: 'success'
                            })
                        }
                    })
                    this.ruleForm.card_reverse_cert = URL.createObjectURL(file.raw)
                } else {
                    this.$message.error('上传图片只能是 JPG或PNG 格式!')
                }
            }

            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 2MB!')
            }
            // 返回图是jpg或png格式并且大小不超过2MB
            return (isJPG || isPNG) && isLt2M
        },
        changePictureUploadBusinessLicense (file, fileList) {
            this.ruleForm.business_cert = URL.createObjectURL(file.raw)
            const isJPG = file.raw.type === 'image/jpeg'
            const isPNG = file.raw.type === 'image/png'
            const isLt2M = file.raw.size / 1024 / 1024 < 2

            if (file.raw) {
                if (isJPG || isPNG) {
                    const fileFormData = new FormData()
                    fileFormData.append('files', file.raw)
                    addIdCard(fileFormData).then(res => { //调用上传接口
                        if (res) {
                            this.ruleForm.business_cert = URL.createObjectURL(file.raw)//接口调用成功后赋值src
                            this.$message({
                                message: '恭喜你，上传成功！',
                                type: 'success'
                            })
                        }
                    })
                    this.ruleForm.business_cert = URL.createObjectURL(file.raw)
                } else {
                    this.$message.error('上传图片只能是 JPG或PNG 格式!')
                }
            }

            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 2MB!')
            }
            // 返回图是jpg或png格式并且大小不超过2MB
            return (isJPG || isPNG) && isLt2M
        },

        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    merchantEnter(this.ruleForm).then(res => {
                        if (res.code === 1) {
                            this.$message.success(res.msg)
                            this.getMerchantData();
                            //this.$router.push({ name: 'index' })
                        } else {
                            this.$message.error(res.msg)
                        }
                    })
                }
            })
        },
        resetForm (formName) {
            this.$refs[formName].resetFields()
        }
    }
}
</script>

<style lang="less" scoped>
.header {
    font-size: 18px;
    color: #333;
    font-weight: 600;
    margin-bottom: 30px;
}

.logo-uploader {
    /deep/.el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        &:hover {
            border-color: #409EFF;
        }
        .logo-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 80px;
            height: 80px;
            line-height: 80px;
            text-align: center;
        }
    }
    .logo {
        width: 80px;
        height: 80px;
        display: block;
    }
}

.avatar-uploader {
    &.idCardFront {
        /deep/.el-upload {
            background-image: url(../../assets/img/idCardFront.png);
            background-repeat: no-repeat;
        }
    }

    &.idCardReverse {
        /deep/.el-upload {
            background-image: url(../../assets/img/idCardReverse.png);
            background-repeat: no-repeat;
        }
    }

    &.businessLicense {
        /deep/.el-upload {
            background-image: url(../../assets/img/businessLicense.png);
            background-repeat: no-repeat;
        }
    }

    /deep/.el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 12px;
        cursor: pointer;
        position: relative;
        overflow: hidden;



        &:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 255px;
            height: 160px;
            line-height: 160px;
            text-align: center;
            opacity: 0;
        }
    }

    .avatar {
        width: 255px;
        height: 160px;
        display: block;
    }
}
</style>